package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.domain.WdyVcBuyOrder;
import com.ruoyi.system.service.IWdyUserService;
import com.ruoyi.system.service.IWdyVcBuyOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * vc购买订单 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-12-16
 */
@Controller
@RequestMapping("/system/wdyVcBuyOrder")
public class WdyVcBuyOrderController extends BaseController
{
    private String prefix = "system/wdyVcBuyOrder";
	
	@Autowired
	private IWdyVcBuyOrderService wdyVcBuyOrderService;
	@Autowired
	private IWdyUserService wdyUserService;

	@RequiresPermissions("system:wdyVcBuyOrder:view")
	@GetMapping()
	public String wdyVcBuyOrder()
	{
	    return prefix + "/wdyVcBuyOrder";
	}
	
	/**
	 * 查询vc购买订单列表
	 */
	@RequiresPermissions("system:wdyVcBuyOrder:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyVcBuyOrder wdyVcBuyOrder)
	{
		startPage();
        List<WdyVcBuyOrder> list = wdyVcBuyOrderService.selectWdyVcBuyOrderList(wdyVcBuyOrder);
		for(WdyVcBuyOrder e : list) {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_id", e.getUserId());
			WdyUser user = wdyUserService.getOne(query);
			if(user != null) {
				e.addExtraData("mobile", user.getUserName());
			}
		}
        return getDataTable(list);
	}
	
	
	/**
	 * 导出vc购买订单列表
	 */
	@RequiresPermissions("system:wdyVcBuyOrder:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyVcBuyOrder wdyVcBuyOrder)
    {
    	List<WdyVcBuyOrder> list = wdyVcBuyOrderService.selectWdyVcBuyOrderList(wdyVcBuyOrder);
        ExcelUtil<WdyVcBuyOrder> util = new ExcelUtil<WdyVcBuyOrder>(WdyVcBuyOrder.class);
        return util.exportExcel(list, "wdyVcBuyOrder");
    }
	
	/**
	 * 新增vc购买订单
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存vc购买订单
	 */
	@RequiresPermissions("system:wdyVcBuyOrder:add")
	@Log(title = "vc购买订单", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyVcBuyOrder wdyVcBuyOrder)
	{
		try {
			wdyVcBuyOrderService.buyVc(wdyVcBuyOrder);
		} catch (Exception e) {
			log.error("添加异常", e);
			return error();
		}
		return success();
	}

	/**
	 * 修改vc购买订单
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyVcBuyOrder wdyVcBuyOrder = wdyVcBuyOrderService.selectWdyVcBuyOrderById(id);
		mmap.put("wdyVcBuyOrder", wdyVcBuyOrder);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存vc购买订单
	 */
	@RequiresPermissions("system:wdyVcBuyOrder:edit")
	@Log(title = "vc购买订单", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyVcBuyOrder wdyVcBuyOrder)
	{		
		return toAjax(wdyVcBuyOrderService.updateWdyVcBuyOrder(wdyVcBuyOrder));
	}
	
	/**
	 * 删除vc购买订单
	 */
	@RequiresPermissions("system:wdyVcBuyOrder:remove")
	@Log(title = "vc购买订单", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyVcBuyOrderService.deleteWdyVcBuyOrderByIds(ids));
	}

	@RequiresPermissions("system:wdyVcBuyOrder:view")
	@PostMapping("/getSum")
	@ResponseBody
	public AjaxResult getSum(WdyVcBuyOrder wdyVcBuyOrder)
	{
		AjaxResult success = success();
		Map<String, BigDecimal> sum = wdyVcBuyOrderService.getSum(wdyVcBuyOrder);
		return success.put("sum", sum);
	}
}
